package DesignPattern.Structure.ProxyMode.CGlibProxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class ProxyFactory implements MethodInterceptor {

    private Trainstation trainstation = new Trainstation();

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("回调方法执行了");
        System.out.println("代售点收取一定代理费用");
        return method.invoke(trainstation, objects);
    }

    public Trainstation getProxyObject() {
        // 创建Enhancer对象，类似于JDK的Proxy类
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Trainstation.class);
        enhancer.setCallback(this);
        return (Trainstation) enhancer.create();
    }
}
